<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类别"
          clearable
          size="small"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发生时间" prop="findTime">
        <el-date-picker
          clearable
          size="small"
          style="width: 200px"
          v-model="queryParams.findTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择发生时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kri:Info:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['kri:Info:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kri:Info:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kri:Info:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="InfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="code" width="150">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">{{
            scope.row.id
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="编号" align="center" prop="code" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="类别" align="center" prop="type" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="部门ID" align="center" prop="deptId" />
      <el-table-column
        label="发生时间"
        align="center"
        prop="findTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.findTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="totalAmount" />
      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-promotion"
            @click="handlePoces(scope.row)"
            v-hasPermi="['activiti:pocess:edit']"
            >提交</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-share"
            @click="historicActivity(scope.row)"
            v-hasPermi="['workflow:leave:edit']"
            >追溯
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改KRI指标信息对话框 -->
    <el-dialog
      :title="detailDialog.title"
      :visible.sync="detailDialog.open"
      width="1200px"
      append-to-body
      :before-close="handleClose"
    >
      <InfoDetailTabs :detailDialog.sync="detailDialog" @doRefresh="getList">
      </InfoDetailTabs>
    </el-dialog>

    <el-dialog
      :title="historicDialog.title"
      :visible.sync="historicDialog.open"
      width="1000px"
      append-to-body
    >
      <div class="chart-wrapper">
        <pocessHistoric :detailDialog.sync="historicDialog"> </pocessHistoric>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import pocessHistoric from "@/views/activiti/pocess/pocessHistoric.vue";
import InfoDetailTabs from "./InfoDetailTabs.vue";
import { listInfo, delInfo, exportInfo } from "@/api/kri/Info";

export default {
  name: "Info",
  components: {
    pocessHistoric,
    InfoDetailTabs,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // KRI指标信息表格数据
      InfoList: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 弹出层标题
      detailDialog: {
        //弹框标题
        title: "",

        //业务id
        id: "",
        open: false,
        //是否是详情页 true：是
        isDisabled: true,

        //工作流控制
        //是否弹出工作流
        hasPoces: false,
        befinitionKey: "lossevent_6",
        //  processDefinitionId: "lossevent_6:19:d25fe605-5712-11eb-bf81-b0359fadfbcass",
        vars: {
          acti_num: 4,
          title: "工作流标题",
          code: "",
          componentName: "LosseventDetail",
        },
      },

      //流程追溯
      historicDialog: {
        //  流程实例  roInstanceId
        id: "",
        title: "",
        open: false,
        // 业务id
        businessId: "",
        befinitionKey: "",
      },

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        type: null,
        status: null,
        deptId: null,
        findTime: null,
        totalAmount: null,
      },

      // 表单参数
      form: {},

      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },

  methods: {
    /** 关闭弹框判断 */
    handleClose(done) {
      if (!this.detailDialog.isDisabled) {
        this.$confirm("确认关闭？")
          .then((_) => {
            done();
          })
          .catch((_) => {});
      } else {
        done();
      }
    },

    /** 查询KRI指标信息列表 */
    /** 查询KRI指标信息列表 */
    getList() {
      //this.initDetailDialog();
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.InfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        type: null,
        status: "0",
        deptId: null,
        findTime: null,
        totalAmount: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.detailDialog.id = "";
      this.detailDialog.open = true;
      this.detailDialog.isDisabled = false;
      this.detailDialog.hasPoces = false;
      this.detailDialog.title = "添加KRI指标信息";
    },

    /** 历史 */
    historicActivity(row) {
      const businessId = row.id;
      this.historicDialog.open = true;
      this.historicDialog.title = "流程追溯";
      this.historicDialog.businessId = businessId;
      this.historicDialog.befinitionKey = this.detailDialog.befinitionKey;
    },

    /** 工作流操作 */
    handlePoces(row) {
      const pocessId = row.id;
      this.detailDialog.id = pocessId;
      this.detailDialog.open = true;
      this.detailDialog.isDisabled = true;
      this.detailDialog.hasPoces = true;
      this.detailDialog.title = "提交审批流程";
      this.detailDialog.vars.code = row.code;
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      const pocessId = row.id;
      this.detailDialog.id = pocessId;

      this.detailDialog.open = true;
      this.detailDialog.isDisabled = true;
      this.detailDialog.hasPoces = false;
      this.detailDialog.title = "详情KRI指标信息";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const pocessId = row.id || this.ids[0];
      this.detailDialog.id = pocessId;
      this.detailDialog.open = true;
      this.detailDialog.isDisabled = false;
      this.detailDialog.hasPoces = false;
      this.detailDialog.title = "修改KRI指标信息";
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除KRI指标信息编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有KRI指标信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportInfo(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
